package com.mdlib.helper.ui.fragments.sect.money;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.entity.MoneyRelateEneity;
import com.mdlib.helper.ui.adapter.MoneyRelateAdapter;
import com.mdlib.helper.ui.widget.springview.DefaultFooter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

import static com.mdlib.helper.ui.UIHelper.BUDGET;
import static com.mdlib.helper.ui.UIHelper.MONEY_RELATE_TYPE;
import static com.mdlib.helper.ui.UIHelper.WITHDRAWAL;

/**
 * 作者：HQY on 16/11/16 16:39
 * 邮箱：hqy_xz@126.com
 */

public class MoneyRelateFragment extends BaseTitleFragment {


    @Bind(R.id.rv_money_list)
    RecyclerView mRvMoneyList;
    @Bind(R.id.sv_relate)
    SpringView mSvRelate;


    private MoneyRelateAdapter mMoneyRelateAdapter;
    private List<MoneyRelateEneity> moneyList = new ArrayList<>();
    private String type;
    private String role = "1";

    private int pageNumber = 1;

    public static MoneyRelateFragment newInStance(String type) {
        MoneyRelateFragment fragment = new MoneyRelateFragment();
        Bundle bundle = new Bundle();
        bundle.putString(MONEY_RELATE_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getString(MONEY_RELATE_TYPE);
        }
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_money_relate;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        if (type.equals(BUDGET)) {
            setTitle(R.string.balance_payment_details, R.color.white)
                    .setTitleBgColor(R.color.generic_view_bg)
                    .setImgLeftListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            removeFragment();
                        }
                    });
        } else if (type.equals(WITHDRAWAL)) {
            setTitle(R.string.balance_withdrawal_details, R.color.white)
                    .setTitleBgColor(R.color.generic_view_bg)
                    .setImgLeftListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            removeFragment();
                        }
                    });
        }
        init();
    }


    public void init() {
        if (AccountModel.getInstance().getRoleType().equals("user")) {
            role = "1";
        } else {
            role = "2";
        }
        mMoneyRelateAdapter = new MoneyRelateAdapter(moneyList,type);
        mRvMoneyList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvMoneyList.setAdapter(mMoneyRelateAdapter);
        mSvRelate.setType(SpringView.Type.FOLLOW);
        mSvRelate.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                pageNumber = 1;
                getRelate();
            }

            @Override
            public void onLoadmore() {
                getRelate();
            }
        });

        mSvRelate.setHeader(new HelperHeads(getActivity()));
        mSvRelate.setFooter(new DefaultFooter(getActivity()));
        getRelate();
    }

    private void getRelate() {
        if (type.equals(BUDGET)) {
            getAccountDetails();
        } else if (type.equals(WITHDRAWAL)) {
            getWithrawsDateils();
        }
    }

    private void getAccountDetails() {
        startLoadingDialog(true);

        addSubscribe(DataManager.getInstance().getUserApi()
                .getAccountDetails(role, pageNumber + "")
                .compose(RxHelper.<ArrayList<MoneyRelateEneity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<MoneyRelateEneity>>() {
                    @Override
                    protected void onSucc(ArrayList<MoneyRelateEneity> moneyRelateEneities) {
                        stopLoadingDialog();
                        pageNumber++;
                        moneyList = moneyRelateEneities;
                        updateList(moneyList);
                        mSvRelate.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                        mSvRelate.onFinishFreshAndLoad();
                    }
                }));
    }

    private void getWithrawsDateils() {
        startLoadingDialog(true);

        addSubscribe(DataManager.getInstance().getUserApi()
                .getWithdrawsDetails(pageNumber + "")
                .compose(RxHelper.<ArrayList<MoneyRelateEneity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<MoneyRelateEneity>>() {
                    @Override
                    protected void onSucc(ArrayList<MoneyRelateEneity> moneyRelateEneities) {
                        stopLoadingDialog();
                        pageNumber++;
                        moneyList = moneyRelateEneities;
                        updateList(moneyList);
                        mSvRelate.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                        mSvRelate.onFinishFreshAndLoad();
                    }
                }));
    }

    public void updateList(List<MoneyRelateEneity> moneyLists) {
        if (moneyLists.size() > 0 && pageNumber == 2) {
            mMoneyRelateAdapter.setNewData(moneyLists);
            mMoneyRelateAdapter.notifyDataSetChanged();
        } else if (moneyLists.size() > 0 && pageNumber > 2) {
            mMoneyRelateAdapter.addData(moneyLists);
            mMoneyRelateAdapter.notifyDataSetChanged();
        } else if (moneyLists.size() <= 0 && pageNumber > 2) {
            pageNumber--;
            ToastUtil.showToast("没有更多数据了!");
        } else {
            ToastUtil.showToast("没有找到你需要的数据！");
        }
    }
}
